package com.hup.utils.swing.customView;

import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.GraphicsUtil;
import com.hup.utils.swing.ImageUtil;
import lombok.Getter;
import lombok.Setter;
import sun.swing.SwingUtilities2;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/**
 * 代码摘自 {@link ImageButton}
 * .支持背景色和背景图共存
 * .支持缩放比例
 * .支持idea的GUI
 * .有 {@link JLabel} 的默认文字功能
 *
 * @author hugan
 * @date 2022/10/2
 */
@SuppressWarnings("DuplicatedCode")
public class ImageLabel extends JLabel {

    /**
     * 图标尺寸单位:以默认字体的高度作为一个单位
     */
    private static final int SIZE_UNIT = SwingUtilities2.getFontMetrics(null, LookThemeUtil.getDefaultScaledFont()).getHeight();
    /**
     * 图标图案距离(最近)边缘的padding
     */
    @Setter
    @Getter
    private int padding = 5;
    /**
     * 是否平铺图标
     */
    @Getter
    private boolean fullIcon = false;

    /**
     * 原始图标
     */
    @Getter
    private BufferedImage iconImage;
    /**
     * 图标的宽高比例=w/h
     */
    private float iconRatio;
    /**
     * 图标绘制范围
     */
    @Getter
    private Rectangle iconRect = new Rectangle();

    /**
     * 图标尺寸(含四边padding):以 {@link #SIZE_UNIT} 作为单位的;
     * 默认宽高都是[1.5个字的高度];
     */
    @Getter
    private float unitedIconWidth = 1.5f;
    @Getter
    private float unitedIconHeight = 1.5f;


    public ImageLabel() {
        setUnitedIconSize(unitedIconWidth, unitedIconHeight);
    }

    @Override
    public void setOpaque(boolean isOpaque) {
        super.setOpaque(false);
    }

    /**
     * {@link #unitedIconWidth}
     */
    public void setUnitedIconWidth(float unitedIconWidth) {
        setUnitedIconSize(unitedIconWidth, unitedIconHeight);
    }

    /**
     * {@link #unitedIconWidth}
     */
    public void setUnitedIconHeight(float unitedIconHeight) {
        setUnitedIconSize(unitedIconWidth, unitedIconHeight);
    }

    /**
     * {@link #unitedIconWidth}
     */
    public void setUnitedIconSize(float unitedIconWidth, float unitedIconHeight) {
        this.unitedIconWidth = unitedIconWidth;
        this.unitedIconHeight = unitedIconHeight;
        updateSize();
    }

    private void updateSize() {
        if (getFont() == null) return;//父类初始化时,变量为空
        Dimension size = new Dimension((int) (SIZE_UNIT * unitedIconWidth), (int) (SIZE_UNIT * unitedIconHeight));
        setPreferredSize(size);
        setMinimumSize(size);

        revalidate();
        repaint();
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        updateImageRect();
    }

    /**
     * 本方法支持idea的GUI
     */
    @Override
    public void setIcon(Icon icon) {
        Image newIcon = icon == null ? null : ((ImageIcon) icon).getImage();
        setBackgroundImage(newIcon);
    }

    public void setBackgroundImage(Image newIcon) {
        if (this.iconImage == newIcon) return;
        if (this.iconImage != null) this.iconImage.flush();
        this.iconImage = newIcon == null ? null : ImageUtil.getImage(newIcon);

        //图标的宽高比例=w/h
        if (this.iconImage == null) {
            iconRatio = 0f;
        } else {
            int iconWidth = this.iconImage.getWidth(null);
            int iconHeight = this.iconImage.getHeight(null);
            iconRatio = (float) (iconWidth / (iconHeight + 0d));
        }
        updateImageRect();
    }

    public void setFullIcon(boolean fullIcon) {
        this.fullIcon = fullIcon;
        updateImageRect();
    }

    private void updateImageRect() {
        int width = getWidth();
        int height = getHeight();
        if (width == 0 || height == 0 || iconRatio == 0) {
            iconRect.setBounds(0, 0, 0, 0);
            return;
        }

        if (fullIcon) {
            iconRect.setBounds(padding, padding, width - padding * 2, height - padding * 2);
            return;
        }

        int iconW = width - padding * 2;
        int iconH = height - padding * 2;
        //处理<=0
        if (iconW <= 0) iconW = 1;//避免无图像,或分母为零
        if (iconH <= 0) iconH = 1;
        //修正比例后的尺寸
        double rParent = iconW / (iconH + 0d);
        if (rParent < iconRatio) {
            iconH = (int) (iconW / iconRatio);
        } else {
            iconW = (int) (iconH * iconRatio);
        }
        int iconX = (width - iconW) / 2;
        int iconY = (height - iconH) / 2;
        iconRect.setBounds(iconX, iconY, iconW, iconH);
    }

    @Override
    protected void paintComponent(Graphics g0) {
        Graphics2D g = GraphicsUtil.antiAlias((Graphics2D) g0);
        int width = getWidth();
        int height = getHeight();
        if (width == 0 || height == 0) return;

        //背景色
        Color background = getBackground();
        if (background != null) {
            g.setColor(background);
            g.fillRect(0, 0, width, height);
        }

        //图标/背景图
        g.drawImage(this.iconImage,
                iconRect.x, iconRect.y, iconRect.width, iconRect.height,
                null);

        //默认文本
        super.paintComponent(g);
    }

}
